package com.maxatime.web.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.maxatime.exception.PhoenixException;
import com.maxatime.pojo.*;
import com.maxatime.service.CurriculumService;
import com.maxatime.utils.MyContent;
import com.maxatime.utils.SerialNumberUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Array;
import java.util.*;

/**
 * @author Mr.Zhao
 * @title:
 * @projectName phoenix
 * @description: TODO
 * @date 2021/8/26 19:18
 */
@Controller
public class CurriculumController {
    Logger log = LogManager.getLogger();
    @Autowired
    private CurriculumService curriculumService;

    @RequestMapping("/curriculumFind")
    @ResponseBody
    public Map<String, Object> curriculumFind(@RequestParam(required = false, defaultValue = "1") int page,
                                             @RequestParam(required = false, defaultValue = "7") int limit,
                                              CurriculumVo vo


    ) {

        // 使用Pagehelper传入当前页数和页面显示条数会自动为我们的select语句加上limit查询
        // 从他的下一条sql开始分页
        PageHelper.startPage(page, limit);
        List<Curriculum> dor = curriculumService.curriculumFind(vo);// 这是我们的sqlf


//        for (int i = 0; i < dor.size(); i++) {
//            Integer  ww=    curriculumService.selectnownum(dor.get(i).getDormitoryId());
//
//            dor.get(i).setNowNumber(ww);
//
//        }
        PageInfo<Curriculum> rolePageInfo = new PageInfo<>(dor);//
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", rolePageInfo.getTotal());
        map.put("data", rolePageInfo.getList());
        log.info("map------------------------------------" + map);
        return map;

    }
    @RequestMapping("/curr")
    public  String curr(){
        return "/html/curriculum";
    }

    @RequestMapping("/curriculumDelete")
    @ResponseBody
    public Integer curriculumDelete(@RequestBody String data) {
        JSONObject jsonObject = JSONObject.parseObject(data);
        String pk = jsonObject.getString("pkId");
        int pkId = Integer.parseInt(pk);


        return curriculumService.curriculumDelete(pkId);


    }

    @RequestMapping("/codeXiaoyan")
    @ResponseBody
    public Integer codeXiaoyan(String curriculumCode){

        List<Curriculum> dor = curriculumService.curriculumFindd();
        for (int i=0;i<dor.size();i++){

            if (dor.get(i).getCurriculumCode().equals(curriculumCode)){
                return 1;
            }
        }
           return 0;

    }

  @RequestMapping("/updatecurr")
  @ResponseBody
  public Map<String, Object> updatecurr(@RequestBody Curriculum curriculum) {
      Map<String, Object> m = new HashMap<String, Object>();


      Calendar now = Calendar.getInstance();//获取时间
      curriculum.setUpdateTime(now.getTime());//当前时间

      try {
          List<Curriculum> dor = curriculumService.curriculumFindd();
          for (int i=0;i<dor.size();i++){

              if (dor.get(i).getCurriculumCode().equals(curriculum.getCurriculumCode())){
                  m.put(MyContent.CODE, "9999");
                  m.put(MyContent.MSG, "课程代码已存在");
              }
              else {
                  Integer cc = curriculumService.updatecurr(curriculum);
                  m.put(MyContent.CODE, "0000");
                  m.put(MyContent.MSG, "修改成功");
                  m.put("data", cc);
              }
          }



      } catch (PhoenixException e) {
          e.printStackTrace();
          log.error("exception::" + e.getMessage());
          log.error("exception--code" + e.getCode());
          log.error("exception--msg" + e.getMsg());
          m.put(MyContent.CODE, e.getCode());
          m.put(MyContent.MSG, e.getMsg());
          m.put("data", 0);
      }

      return m;
  }
  @RequestMapping("/insertCurriculum")
  @ResponseBody
  public Map<String, Object> insertCurriculum(@RequestBody Curriculum curriculum,HttpSession session) {
      Map<String, Object> m = new HashMap<String, Object>();



      try {
          List<Curriculum> dor = curriculumService.curriculumFindd();
          for (int i = 0; i < dor.size(); i++) {

              if (dor.get(i).getCurriculumCode().equals(curriculum.getCurriculumCode())) {
                  m.put(MyContent.CODE, "9999");
                  m.put(MyContent.MSG, "课程代码已存在");
              } else {
                  Integer cc = curriculumService.insertCurriculum(curriculum);
                  m.put(MyContent.CODE, "0000");
                  m.put(MyContent.MSG, "添加成功");
                  m.put("data", cc);
              }
          }
          } catch(PhoenixException e){
              e.printStackTrace();
              log.error("exception::" + e.getMessage());
              log.error("exception--code" + e.getCode());
              log.error("exception--msg" + e.getMsg());
              m.put(MyContent.CODE, e.getCode());
              m.put(MyContent.MSG, e.getMsg());
              m.put("data", 0);
          }

          return m;


  }
  @RequestMapping("/deleteListCurr")
    @ResponseBody
    public Integer deleteListCurr(@RequestBody String jsonStr){
      JSONObject jsonObject = JSONObject.parseObject(jsonStr);
      JSONArray pkId = jsonObject.getJSONArray("pkId");
      Integer[] pkid= new Integer[pkId.size()];
      for (int i=0;i<pkId.size();i++){
          pkid[i] =Integer.parseInt(pkId.get(i).toString());
      }
      List<Integer> list = Arrays.asList(pkid);

      return curriculumService.deleteListCurr(list);

  }


  @RequestMapping("/getcourse")
  public String getcourse(HttpSession session, Model model) {
      List<TrimesterCurriculumSchedule> list = curriculumService.getcourse();
      log.info("ddddddddddddddddddddddddd"+list);
      Random random = new Random();

      for (int i = 0; i <list.size(); i++) {
          random.nextInt(list.get(i).getCurriculumName().length());

      }

//      for (int i=0;i<list.size();i++){
//
         Collections.shuffle(list,random);
//      }

      model.addAttribute("list",list);
      return "/html/course";
  }







}
